package com.dmzc.stage2.chapter27;

import com.dmzc.stage2.chapter19.Future;

import java.util.HashMap;
import java.util.Map;

public class OrderServiceProxy implements OrderService {
    private final OrderService orderService;
    private final ActiveMessageQueue activeMessageQueue;

    public OrderServiceProxy(OrderService orderService, ActiveMessageQueue activeMessageQueue) {
        this.orderService = orderService;
        this.activeMessageQueue = activeMessageQueue;
    }

    /**
     * 根据订单编号查询订单明细，有入参也有返回值，返回类型必须是Future
     *
     * @param orderId
     * @return
     */
    @Override
    public Future<String> findOrderDetails(long orderId) {
        final ActiveFuture<String>activeFuture=new ActiveFuture<>();
        Map<String,Object>params=new HashMap<>();
        params.put("orderId",orderId);
        params.put("activeFuture",activeFuture);
        MethodMessage message=new FindOrderDetailsMessage(params,orderService);
        activeMessageQueue.offer(message);
        return activeFuture;
    }

    /**
     * 提交订单
     *
     * @param account
     * @param orderId
     */
    @Override
    public void order(String account, long orderId) {
        Map<String,Object>params=new HashMap<>();
        params.put("account",account);
        params.put("orderId",orderId);
        MethodMessage message=new OrderMessge(params,orderService);
        activeMessageQueue.offer(message);
    }
}
